/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stefanini.resultmundial2014.ejb;

import com.stefanini.mundial2014capadatos.Usuario;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**/
@LocalBean
@Stateless
public class UserManager {
    /*Insercion de entity manager*/
    @PersistenceContext

    /*Declaracion  de entity manager*/
    private EntityManager em;
    
    /* Metedo que verifica que el usuario existe */
    public Usuario verificarLogin(String usuario, String password){
        try {
            StringBuilder query= new StringBuilder();
            query.append(" SELECT u FROM Usuario u ");
            query.append("WHERE u.username= :USERNAME ");
            query.append("AND u.password = :PASSWORD ");
            
            /**/
            Query q = em.createQuery(query.toString(),Usuario.class);
            q.setParameter("USERNAME", usuario);
            q.setParameter("PASSWORD", password);
            /*q.getResultList();*/
            return (Usuario)q.getSingleResult();
        } catch (Exception e) {
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, 
                    "Error al verificar el login",e);
            return null;
        }       
    }
}